package com.dashu.core.service;


import com.dashu.base.enums.AtStatus;
import com.dashu.base.utils.VerificationUtil;
import com.dashu.core.dto.company.HomeInfoDTO;
import com.dashu.core.model.Company;
import com.dashu.core.model.CompanyPersonal;
import com.dashu.core.model.CompanyTeacher;
import com.dashu.core.model.User;
import com.dashu.core.modelat.Distinguish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;


@Service
public class HomeService extends DataService {


    @Autowired
    private AtService atService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyPersonalService companyPersonalService;
    @Autowired
    private CompanyTeacherService companyTeacherService;


    @Transactional
    public HomeInfoDTO homeInfo() throws Exception {
        long userId = Long.parseLong(VerificationUtil.getUserId());
        List<User> users = userService.BaseUser(userId);
        List<Long> creteUsers = users.stream().map(User::getId).collect(Collectors.toList());
        creteUsers.add(userId);
        List<Company> companies = companyService.creteUserAll(creteUsers);
        List<CompanyPersonal> companyPersonals = companyPersonalService.findByCompanyAll(companies.stream().map(Company::getId).collect(Collectors.toList()));
        List<CompanyTeacher> companyTeachers = companyTeacherService.findByCompanyAll(companies.stream().map(Company::getId).collect(Collectors.toList()));
        HomeInfoDTO homeInfoDTO = new HomeInfoDTO();
        homeInfoDTO.setTeacherCount(companyTeachers.size());
        homeInfoDTO.setStudentCount(companyPersonals.size());
        homeInfoDTO.setStudentAtCount(companyPersonals.stream().filter(ct -> ct.getAttendanceId() != 0).count());
        List<Distinguish> list = atService.findInfoList(companies.stream().map(Company::getId).collect(Collectors.toList()));

        List<Distinguish> normal = list.stream().filter(d -> d.getStatus().equals(AtStatus.NORMAL.name())).collect(Collectors.toList())
                .stream().collect(collectingAndThen(toCollection(() ->
                        new TreeSet<>(Comparator.comparing(Distinguish::getCId))), ArrayList::new));
        homeInfoDTO.setNormal(normal.size());
        List<Distinguish> late = list.stream().filter(d -> d.getStatus().equals(AtStatus.LATE.name())).collect(Collectors.toList())
                .stream().collect(collectingAndThen(toCollection(() ->
                        new TreeSet<>(Comparator.comparing(Distinguish::getCId))), ArrayList::new));
        homeInfoDTO.setLate(late.size());
        List<Distinguish> lack = list.stream().filter(d -> d.getStatus().equals(AtStatus.LACK.name())).collect(Collectors.toList())
                .stream().collect(collectingAndThen(toCollection(() ->
                        new TreeSet<>(Comparator.comparing(Distinguish::getCId))), ArrayList::new));
        homeInfoDTO.setLack(lack.size());
        List<Distinguish> leave = list.stream().filter(d -> d.getStatus().equals(AtStatus.LEAVE.name())).collect(Collectors.toList())
                .stream().collect(collectingAndThen(toCollection(() ->
                        new TreeSet<>(Comparator.comparing(Distinguish::getCId))), ArrayList::new));
        homeInfoDTO.setLeave(leave.size());
        return homeInfoDTO;

    }

}
